home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Scripts / DoIt < prev    next >
Encoding:
Text File  |  1993-08-23  |  6.0 KB  |  203 lines  |  [TEXT/MPS ]

  1. #
  2. #    NAME:
  3. #        DoIt
  4. #
  5. #    SYNTAX:
  6. #        DoIt (CommandFile [-echo] [-dump]) | [-selection]
  7. #
  8. #    SYNOPSIS:
  9. #        DoIt will execute a series of shell commands hilighting each command
  10. #        as it is executed.  The commands can be in either a file or the
  11. #        current selection.  If a CommandFile is passed to doit the file is
  12. #        opened (as the active window) and each line (multiple lines if a "∂"
  13. #        is present at the end of a line) is executed.  The window is left open
  14. #        if it was already open or closed if it was not.
  15. #
  16. #    OPTIONS:
  17. #        -e[cho]            Each command is echo to the Worksheet before execution.
  18. #        -d[ump]            If an error occurs in one of the commands all the
  19. #                            remaining commands (including the command that failed)
  20. #                            are written to the Worksheet and marked by "ToDo".
  21. #        -s[election]    Execute the commands in the current selection.
  22. #        -c[ontinue]        Don't stop executing commands if any return bad status.
  23. #
  24. #    SPECIAL NOTE:
  25. #        The initial overhead is fairly substantial (on a MacPlus) when executing
  26. #        commands from the current selection.  For a 10 line selection it takes
  27. #        26 seconds of "pre-processing" before DoIt starts to execute the
  28. #        first command.  If the same 10 lines were in a file the overhead is
  29. #        only 11 seconds.  Once execution begins the overhead is small in either
  30. #        case.
  31. #
  32. #        This script does not work correctly on structured commands.
  33. #
  34. #    AUTHOR:
  35. #        Peter J. Potrebic
  36. #        Copyright Apple Computer, Inc. 1988-1990, 1993
  37. #        All Rights Reserved.
  38. #
  39. #
  40. #    Revision History:
  41. #
  42. #    07/26/93    GAB    Removed explicit quoting from Files command.
  43. #
  44.  
  45. Unset ErrFile
  46. Unset CommandFile
  47. Unset EchoCmds
  48. Unset DumpCmds
  49. Unset CurSel
  50. Unset DontStop
  51.  
  52. Set OldExit {Exit}
  53. Set Cmd "{0}"
  54. Set Syntax "(CommandFile [-echo] [-dump]) | [-selection]"
  55. Set Error 0
  56.  
  57. For Arg In {"Parameters"}                            # Parse the command line
  58.  
  59.     # Checking for the echo option - echo commands before they are executed
  60.     If "{Arg}" == "-e" || "{Arg}" == "-echo"
  61.         If {EchoCmds} == 0
  62.             Set EchoCmds 1
  63.         Else
  64.             Echo "### {Cmd} - option ∂"{Arg}∂" multiply defined"
  65.             Set Error 1
  66.         End
  67.  
  68.     # Checking for the dump option - echo unexecuted commands after error
  69.     Else If "{Arg}" == "-d" || "{Arg}" == "-dump"
  70.         If {DumpCmds} == 0
  71.             Set DumpCmds 1
  72.         Else
  73.             Echo "### {Cmd} - option ∂"{Arg}∂" multiply defined"
  74.             Set Error 1
  75.         End
  76.     
  77.     # Checking for the selection option - commands are in the current selection
  78.     Else If "{Arg}" == "-s" || "{Arg}" == "-selection"
  79.         If {CurSel} == 0
  80.             Set CurSel 1
  81.         Else
  82.             Echo "### {Cmd} - option ∂"{Arg}∂" multiply defined"
  83.             Set Error 1
  84.         End            
  85.  
  86.     # Checking for the selection option - commands are in the current selection
  87.     Else If "{Arg}" == "-c" || "{Arg}" == "-continue"
  88.         If {DontStop} == 0
  89.             Set DontStop 1
  90.         Else
  91.             Echo "### {Cmd} - option ∂"{Arg}∂" multiply defined"
  92.             Set Error 1
  93.         End
  94.  
  95.     # Found unknown option
  96.     Else If "{Arg}" =~ /-≈/ || "{Arg}" == -
  97.         Echo "### {Cmd} - ∂"{Arg}∂" is not an option"
  98.         Set Error 1
  99.     
  100.     # Found the command file
  101.     Else
  102.         If "{CommandFile}" == 0
  103.             Set CommandFile "{Arg}"
  104.         Else
  105.             Echo "### {Cmd} - Too many parameters were specified."
  106.             Set Error 1
  107.         End
  108.     End
  109. End > Dev:StdErr
  110.  
  111. If {CurSel} && ({DumpCmds} || {EchoCmds})
  112.     Echo "### {Cmd} - conflicting options were specified"
  113.     Set Error 1
  114. End > Dev:StdErr
  115.  
  116. If ("{CommandFile}" == "" && !{CurSel})
  117.     Echo "### {Cmd} - No command file specified"
  118.     Set Error 1
  119. End > Dev:StdErr
  120.  
  121. If {Error}                                            # Were there any syntax errors?
  122.     Echo "# Usage - {Cmd} {Syntax}"
  123.     Exit {Error}                                    # Exiting (because of syntax error)
  124. End > Dev:StdErr
  125.  
  126. Set Exit 0                                                    # we’ll handle the errors, thank you
  127.  
  128. #
  129. #                EXECUTING THE CURRENT SELECTION...
  130. #
  131. If {CurSel}
  132.     Set MyActive "{Active}"
  133.     Mark -y § DoIt "{MyActive}"                        # mark all the commands to be executed
  134.     Set NumCmds `Count -l "{MyActive}".§`
  135.     
  136.     If {NumCmds} > 0
  137.         Set NumEscapes `Search -q /≈∂∂∞/ "{MyActive}".§ ≥ Dev:Null | Count -l`
  138.         Evaluate NumCmds = {NumCmds} - {NumEscapes}
  139.     
  140.         Find ΔDoIt "{MyActive}"                            # Goto top of Commands to be executed
  141.         
  142.         Set cCmd 0                                        # Initialize command counter
  143.         Loop
  144.             Find /•/ "{MyActive}"                    # Find beginning of next line
  145.             Find §:/[¬∂∂]∞/ "{MyActive}"            # Find from § to end of command
  146.             Evaluate cCmd += 1                        # Increment command counter
  147.             Execute "{MyActive}".§                    # Execute the command
  148.             Set commandStatus {Status}
  149.             If {commandStatus} ≠ 0
  150.                 Set Error {commandStatus}                    # Capture and save status
  151.             End
  152.             Break If ((!{DontStop} && {Error} ≠ 0) || {cCmd} == {NumCmds})
  153.         End
  154.         Find §!1 "{MyActive}"                        # go to line after last command
  155.     End
  156.     UnMark DoIt "{MyActive}"
  157.  
  158. #
  159. #                EXECUTING FROM A FILE...
  160. #
  161. Else
  162.     Set ToDo "#### Still ToDo… ####"
  163.     Set FullCommandFile `Files -i -fr "{CommandFile}"`
  164.     Set WasOpen `Evaluate "∂`Windows∂`" =~ /≈{FullCommandFile}≈/`
  165.     Open "{FullCommandFile}"                            # Open command file
  166.     Exit {Status} If {Status} ≠ 0
  167.     Find • "{FullCommandFile}"                            # Goto top of File to be executed
  168.     Loop                                                        # Loop through all commands
  169.         Find /•/ "{FullCommandFile}"                    # Find beginning of next line
  170.         Break If {Status}    ≠ 0                            # At end of file
  171.         Find §:/[¬∂∂]∞/ "{FullCommandFile}"            # Find from § to end of command
  172.         Break If {Status}    ≠ 0                            # At end of file
  173.         If {EchoCmds}
  174.             Begin
  175.             Catenate "{FullCommandFile}".§            # Write command to Standard Output
  176.             Echo                                                # Echo a newline after the command
  177.         End >> "{Worksheet}".§
  178.         End
  179.         Execute "{FullCommandFile}".§                    # Execute the command
  180.         Set commandStatus {Status}
  181.         If {commandStatus} ≠ 0
  182.             Set Error {commandStatus}                    # Capture and save status
  183.         End
  184.         If {Error} ≠ 0                                        # There was an error
  185.             If {DumpCmds}                                    # List the commands left to execute
  186.                 Begin
  187.                     Echo "{ToDo}"                            # List includes the command that failed
  188.                     Find §:∞ "{FullCommandFile}"        # Select the unexecuted commands
  189.                     Catenate "{FullCommandFile}".§    # Write commands to Standard Output
  190.                     Echo
  191.                 End >> "{Worksheet}".§
  192.                 Mark -y § ToDo "{Worksheet}"            # Mark the commands that weren't executed
  193.             End
  194.             Break If {OldExit}
  195.         End
  196.     End
  197.  
  198.     If ¬{WasOpen}
  199.         Close "{FullCommandFile}" ≥ Dev:Null        # Close the command file
  200.     End
  201. End
  202. Exit "{Error}"                                                # return with proper status
  203.